package com.ruoyi.system.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.domain.entity.SysDeopCycle;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.DetectionCycle;
import com.ruoyi.system.domain.Person;
import com.ruoyi.system.domain.Ptesting;
import com.ruoyi.system.domain.vo.DetectionCycleTimeVo;
import com.ruoyi.system.domain.vo.IndexVo;
import com.ruoyi.system.domain.vo.PersonVo;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.service.IPersonService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.IndexService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;

import static com.ruoyi.common.utils.SecurityUtils.getDeptId;
import static com.ruoyi.common.utils.SecurityUtils.getUsername;

/**
 * personService业务层处理
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Service
public class IndexServiceImpl implements IndexService {
    @Autowired
    private IndexMapper indexMapper;

    @Autowired
    private DetectionCycleMapper detectionCycleMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;


    /**
     * 获取类别当前期数/跟重点人群统计图是公用的接口
     *
     * @return 当前期数
     */
    public Map<String, Object> getLeiBie(IndexVo indexVo) {
        DetectionCycle detectionCycle1 = new DetectionCycle();
        if (indexVo.getDeptId() == null) {
            detectionCycle1.setDeptId(getDeptId());
        }
        //根据登录用户获取部门id查看部门分配的类别
        List<DetectionCycle> list = new ArrayList<>();
        if (StringUtils.isNotNull(detectionCycle1.getDeptId()) && detectionCycle1.getDeptId() != 100L) {
            list = detectionCycleMapper.selectCycleDeptList(detectionCycle1);
        } else {
            list = detectionCycleMapper.selectDetectionCycleList(detectionCycle1);
        }
        List categories = new ArrayList(); //类别名称
        List yingwancheng = new ArrayList(); //应完成
        List weiwancheng = new ArrayList(); //未完成
        for (DetectionCycle detectionCycle : list) {
            Map mao = new HashMap();
            mao.put("value", detectionCycle.getCategoryName());
            mao.put("id", detectionCycle.getId());
            categories.add(mao);
            //当前类别期数
            DetectionCycleTimeVo detectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(DateUtils.dateTime(new Date()), detectionCycle.getId()));
            if (detectionCycleTimeVo == null) {
                yingwancheng.add(0);
                weiwancheng.add(0);
            } else {
                Map map = indexMapper.selectCountYing(new IndexVo(detectionCycleTimeVo.getId()));
                if (map != null) {
                    yingwancheng.add(StringUtils.isNotNull(map.get("frequency")) ? map.get("frequency") : 0);
                    if (StringUtils.isNotNull(map.get("frequency")) && StringUtils.isNotNull(map.get("frequencyAlready"))) {
                        int frequency = Integer.parseInt(map.get("frequency") + "");
                        int frequencyAlready = Integer.parseInt(map.get("frequencyAlready") + "");
                        if (frequencyAlready > frequency) {
                            weiwancheng.add(0);
                        } else {
                            weiwancheng.add(StringUtils.isNotNull(map.get("frequency")) ? (Integer.parseInt(map.get("frequency") + "") - (StringUtils.isNotNull(map.get("frequencyAlready")) ? Integer.parseInt(map.get("frequencyAlready") + "") : 0)) : 0);
                        }
                    }
                } else {
                    yingwancheng.add(0);
                    weiwancheng.add(0);
                }
            }
        }
        Map yingwanchengmap = new HashMap();
        yingwanchengmap.put("data", yingwancheng);
        yingwanchengmap.put("name", "总人次");
        Map weiwanchengmap = new HashMap();
        weiwanchengmap.put("data", weiwancheng);
        weiwanchengmap.put("name", "未完成人次");
        List series = new ArrayList();
        series.add(yingwanchengmap);
        series.add(weiwanchengmap);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("categories", categories);
        objectObjectHashMap.put("series", series);
        return objectObjectHashMap;
    }


    /**
     * 获取部门是否有下级
     */
    public List<SysDept> getZhongDianRenQun() {
        return sysDeptMapper.selectParentId(getDeptId());
    }


    /**
     * 行业分类
     */
    public Map<String, Object> getHnagYeFenLei(IndexVo indexVo) {
        if (indexVo.getDeptId() == null) {
            indexVo.setDeptId(getDeptId());
        }
        List hangyes = new ArrayList(); //行业名称
        List yingwancheng = new ArrayList(); //应完成
        List weiwancheng = new ArrayList(); //未完成
        List<SysDept> sysDepts = new ArrayList<>();
        sysDepts = sysDeptMapper.selectParentId(indexVo.getDeptId()); //获取当前登录用户所负责的部门
        if (sysDepts.size() == 0) {
            SysDept sysDepts1 = sysDeptMapper.selectDeptById(indexVo.getDeptId());
            sysDepts.add(sysDepts1);
        }
        for (SysDept dept : sysDepts) {
            Map mao = new HashMap();
            mao.put("value", dept.getDeptName());
            mao.put("deptId", dept.getDeptId());
            mao.put("type", "getHnagYeFenLei");
            hangyes.add(mao);
            List<SysDeopCycle> sysDeopCycles = sysDeptMapper.selectDeptCycleById(dept.getDeptId());  //根据部门获取当前当前部门关联的类别
            if (sysDeopCycles.size() == 0) {
                yingwancheng.add(0);
                weiwancheng.add(0);
            } else {
                int frequencys = 0;
                int frequencyAlreadys = 0;
                for (SysDeopCycle sysDeopCycle : sysDeopCycles) {
                    DetectionCycleTimeVo detectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(DateUtils.dateTime(new Date()), sysDeopCycle.getCycleId()));
                    if (detectionCycleTimeVo == null) {
                        continue;
                    } else {
                        Map map = indexMapper.selectCountYing(new IndexVo(detectionCycleTimeVo.getId(), dept.getDeptId()));
                        if (map != null) {
                            if (StringUtils.isNotNull(map.get("frequency")) && StringUtils.isNotNull(map.get("frequencyAlready"))) {
                                int frequency = Integer.parseInt(map.get("frequency") + "");
                                frequencys += frequency;
                                int frequencyAlready = Integer.parseInt(map.get("frequencyAlready") + "");
                                if (frequencyAlready > frequency) {
                                    frequencyAlreadys += frequency;
                                } else {
                                    frequencyAlreadys += frequencyAlready;
                                }
                            }
                        } else {
                            continue;
                        }
                    }
                }
                yingwancheng.add(frequencys);
                weiwancheng.add(frequencys - frequencyAlreadys);
            }
        }
        Map yingwanchengmap = new HashMap();
        yingwanchengmap.put("data", yingwancheng);
        yingwanchengmap.put("name", "总人次");
        Map weiwanchengmap = new HashMap();
        weiwanchengmap.put("data", weiwancheng);
        weiwanchengmap.put("name", "未完成人次");
        List series = new ArrayList();
        series.add(yingwanchengmap);
        series.add(weiwanchengmap);
        HashMap<String, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("categories", hangyes);
        objectObjectHashMap.put("series", series);
        return objectObjectHashMap;
    }


    public Map<String, Object> getGaoJiSouSuo(IndexVo indexVo) {
        Map M = new HashMap<>();
        List list1 = new ArrayList();
        Map Wmap = new HashMap();
        Map Ymap = new HashMap();
        int frequency = 0;   //应完成
        int frequencyAlready = 0;   //已完成
        if (indexVo.getDetectionCycleId() == null && StringUtils.isEmpty(indexVo.getStarTime()) && StringUtils.isEmpty(indexVo.getEndTime())) {
            List<DetectionCycle> list = new ArrayList<>();
            DetectionCycle detectionCycle1 = new DetectionCycle();
            detectionCycle1.setDeptId(getDeptId());
            if (StringUtils.isNotNull(detectionCycle1.getDeptId()) && detectionCycle1.getDeptId() != 100L) {
                list = detectionCycleMapper.selectCycleDeptList(detectionCycle1);
            } else {
                list = detectionCycleMapper.selectDetectionCycleList(detectionCycle1);
            }
            if (list.size() > 0) {
                DetectionCycleTimeVo detectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(DateUtils.dateTime(new Date()), list.get(0).getId()));
                if (detectionCycleTimeVo != null) {
                    Map map = indexMapper.selectCountYing(new IndexVo(detectionCycleTimeVo.getId()));
                    if (map != null) {
                        if (StringUtils.isNotNull(map.get("frequency")) && StringUtils.isNotNull(map.get("frequencyAlready"))) {
                            frequency = Integer.parseInt(map.get("frequency") + "");
                            frequencyAlready = Integer.parseInt(map.get("frequencyAlready") + "");
                            if (frequencyAlready > frequency) {
                                frequencyAlready = Integer.parseInt(map.get("frequency") + "");
                            }
                        }
                    }
                }
                M.put("detectionCycleTimeVo", detectionCycleTimeVo);
            }
        } else {
            //根据类别id和时间查询
            DetectionCycleTimeVo KdetectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(indexVo.getStarTime(), indexVo.getDetectionCycleId()));
            DetectionCycleTimeVo JdetectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(indexVo.getEndTime(), indexVo.getDetectionCycleId()));
            if (KdetectionCycleTimeVo != null) {
                indexVo.setStarTime(KdetectionCycleTimeVo.getStarTime());
            }
            if (JdetectionCycleTimeVo != null) {
                indexVo.setEndTime(JdetectionCycleTimeVo.getEndTime());
            }
            List<DetectionCycleTimeVo> detectionCycleTimeVos = indexMapper.selectDetectionCycleList(indexVo);
            for (DetectionCycleTimeVo detectionCycleTimeVo : detectionCycleTimeVos) {
                Map map = indexMapper.selectCountYing(new IndexVo(detectionCycleTimeVo.getId()));
                if (map != null) {
                    if (StringUtils.isNotNull(map.get("frequency")) && StringUtils.isNotNull(map.get("frequencyAlready"))) {
                        frequency += Integer.parseInt(map.get("frequency") + "");
                        if (Integer.parseInt(map.get("frequencyAlready") + "") > frequency) {
                            frequencyAlready += frequency;
                        } else {
                            frequencyAlready += Integer.parseInt(map.get("frequencyAlready") + "");
                        }
                    }
                }
            }
        }
        //计算应完成未完成
        Wmap.put("value", frequency - frequencyAlready);
        Wmap.put("name", "未完成");
        Ymap.put("value", frequencyAlready);
        Ymap.put("name", "已完成");
        list1.add(Wmap);
        list1.add(Ymap);
        M.put("tubiao", list1);
        if (frequencyAlready == 0 && frequency == 0) {
            M.put("wanclv", 0);
        } else {
            NumberFormat numberFormat = NumberFormat.getInstance();
            M.put("wanclv", numberFormat.format((float) (frequencyAlready / (float) frequency) * 100));
        }
        return M;
    }

    public List getSuoYouLeiBie(IndexVo indexVo) {
        DetectionCycle detectionCycle1 = new DetectionCycle();
        if (indexVo.getDeptId() == null) {
            detectionCycle1.setDeptId(getDeptId());
            indexVo.setDeptId(getDeptId());
        }
        //根据登录用户获取部门id查看部门分配的类别
        List<DetectionCycle> list = new ArrayList<>();
        if (indexVo.getDetectionCycleId() == null && indexVo.getDeptId() != null) {
            if (StringUtils.isNotNull(detectionCycle1.getDeptId()) && detectionCycle1.getDeptId() != 100L) {
                list = detectionCycleMapper.selectCycleDeptList(detectionCycle1);
            } else {
                list = detectionCycleMapper.selectDetectionCycleList(detectionCycle1);
            }
        } else {
            DetectionCycle detectionCycle = detectionCycleMapper.selectDetectionCycleById(indexVo.getDetectionCycleId());
            list.add(detectionCycle);
        }
        List list1 = new ArrayList();
        for (DetectionCycle detectionCycle : list) {

            List<SysDept> sysDepts = new ArrayList<>();
            indexVo.setDetectionCycleId(detectionCycle.getId());
            sysDepts = indexMapper.selectDeptCycleByIdDept(indexVo);//根据部门获取当前当前部门关联的类别
            if (sysDepts.size() == 0) {
                sysDepts = indexMapper.selectDeptCycleByIdDeptXj(indexVo);//最后一级别
            }
            List hangyes = new ArrayList(); //行业名称
            List yingwancheng = new ArrayList(); //应完成
            List weiwancheng = new ArrayList(); //未完成
            for (SysDept dept : sysDepts) {
                Map mao = new HashMap();
                mao.put("value", dept.getDeptName());
                mao.put("deptId", dept.getDeptId());
                mao.put("detectionCycleId", detectionCycle.getId());
                mao.put("type", "getSuoYouLeiBie");
                hangyes.add(mao);
                List<SysDeopCycle> sysDeopCycles = sysDeptMapper.selectDeptCycleById(dept.getDeptId());  //根据部门获取当前当前部门关联的类别
                if (sysDeopCycles.size() == 0) {
                    yingwancheng.add(0);
                    weiwancheng.add(0);
                    continue;
                } else {
                    int frequencys = 0;
                    int frequencyAlreadys = 0;
                    DetectionCycleTimeVo detectionCycleTimeVo = indexMapper.selectDetectionCycle(new IndexVo(DateUtils.dateTime(new Date()), detectionCycle.getId()));
                    if (detectionCycleTimeVo == null) {
                        continue;
                    } else {
                        Map map = indexMapper.selectCountYing(new IndexVo(detectionCycleTimeVo.getId(), dept.getDeptId()));
                        if (map != null) {
                            if (StringUtils.isNotNull(map.get("frequency")) && StringUtils.isNotNull(map.get("frequencyAlready"))) {
                                int frequency = Integer.parseInt(map.get("frequency") + "");
                                frequencys += frequency;
                                int frequencyAlready = Integer.parseInt(map.get("frequencyAlready") + "");
                                if (frequencyAlready > frequency) {
                                    frequencyAlreadys += frequency;
                                } else {
                                    frequencyAlreadys += frequencyAlready;
                                }
                            }
                        } else {
                            yingwancheng.add(0);
                            weiwancheng.add(0);
                            continue;
                        }
                    }
                    yingwancheng.add(frequencys);
                    weiwancheng.add(frequencys - frequencyAlreadys);
                }
            }
            Map yingwanchengmap = new HashMap();
            yingwanchengmap.put("data", yingwancheng);
            yingwanchengmap.put("name", "总人次");
            Map weiwanchengmap = new HashMap();
            weiwanchengmap.put("data", weiwancheng);
            weiwanchengmap.put("name", "未完成人次");
            List series = new ArrayList();
            series.add(yingwanchengmap);
            series.add(weiwanchengmap);
            HashMap<String, Object> objectObjectHashMap = new HashMap<>();
            objectObjectHashMap.put("categories", hangyes);
            objectObjectHashMap.put("series", series);
            Map map = new HashMap();
            map.put("name", detectionCycle.getCategoryName());
            map.put("value", objectObjectHashMap);
            list1.add(map);
        }
        return list1;
    }

}
